package com.architecture.core.service;

import com.architecture.core.dao.BaseDAO;
import com.architecture.core.entity.BaseEntity;
import com.architecture.core.entity.Resource;
import com.architecture.core.except.ErrException;
import com.architecture.core.service.query.QueryResourceService;
import com.architecture.core.utils.ConfigUtil;
import com.architecture.core.utils.ErrCode;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: aguang
 * Date: 13-1-21
 * Time: 下午2:30
 * To change this template use File | Settings | File Templates.
 */
@Service("baseService")
public class BaseService extends AbstractService {

    @javax.annotation.Resource
    private BaseDAO baseDAO;

    @javax.annotation.Resource
    private QueryResourceService queryResourceService;


    /**
     * 执行源生sql语句查询，boolean类型可以直接填写true或者false
     *
     * @param sql
     * @return
     * @throws ErrException
     */
    public List<Object> executeSQLQuery(String sql) throws ErrException {
        if (StringUtils.isBlank(sql)) {
            throw new ErrException(ErrCode.BLANK_STR);
        }
        return this.baseDAO.executeSQLQuery(sql);
    }

    /**
     * 执行源生sql语句查询
     *
     * @param operaId        操作员id
     * @param categoryType   类型
     * @param permissionType 权限类型
     * @param resCode        资源的唯一代码
     * @param sql            源生语句
     * @return 对象列表
     * @throws ErrException
     */
    public List<Object> executeSQLQuery(String operaId, int categoryType, int permissionType, long resCode, String sql) throws ErrException {
        this.hasPermission(operaId, categoryType, permissionType, resCode);
        return this.executeSQLQuery(sql);
    }

    /**
     * 执行源生sql语句查询
     *
     * @param sql
     * @param valMap 参数，名值对应
     * @return
     * @throws ErrException
     */
    public List<Object> executeSQLQuery(String sql, Map<String, Object> valMap) throws ErrException {
        if (StringUtils.isBlank(sql)) {
            throw new ErrException(ErrCode.BLANK_STR);
        }

        if (CollectionUtils.isEmpty(valMap)) {
            throw new ErrException(ErrCode.EMPTY_COLLECTION);
        }

        return this.baseDAO.executeSQLQuery(sql, valMap);
    }

    /**
     * 执行源生sql语句查询
     *
     * @param operaId        操作员id
     * @param categoryType   类型
     * @param permissionType 权限类型
     * @param resCode        资源的唯一代码
     * @param sql            源生语句
     * @param valMap         参数，名值对应
     * @return
     * @throws ErrException
     */
    public List<Object> executeSQLQuery(String operaId, int categoryType, int permissionType, long resCode, String sql, Map<String, Object> valMap) throws ErrException {
        this.hasPermission(operaId, categoryType, permissionType, resCode);

        return this.executeSQLQuery(sql, valMap);
    }

    /**
     * 执行源生sql语句查询
     *
     * @param sqlList
     * @return
     * @throws ErrException
     */
    public List<List<Object>> executeSQLQuery(List<String> sqlList) throws ErrException {
        if (CollectionUtils.isEmpty(sqlList)) {
            throw new ErrException(ErrCode.EMPTY_COLLECTION);
        }

        return this.baseDAO.executeSQLQuery(sqlList);
    }

    /**
     * 执行源生sql语句查询
     *
     * @param operaId        操作员id
     * @param categoryType   类型
     * @param permissionType 权限类型
     * @param resCode        资源的唯一代码
     * @param sqlList        源生语句
     * @return
     * @throws ErrException
     */
    public List<List<Object>> executeSQLQuery(String operaId, int categoryType, int permissionType, long resCode, List<String> sqlList) throws ErrException {

        this.hasPermission(operaId, categoryType, permissionType, resCode);

        return this.executeSQLQuery(sqlList);
    }

    /**
     * 执行源生sql语句查询,这里相当与批量执行，sqls与valMapList按顺序一一对应
     *
     * @param sqlList
     * @param valMapList
     * @return
     * @throws ErrException
     */
    public List<List<Object>> executeSQLQuery(List<String> sqlList, List<Map<String, Object>> valMapList) throws ErrException {
        if (CollectionUtils.isEmpty(sqlList)) {
            throw new ErrException(ErrCode.EMPTY_COLLECTION);
        }

        if (CollectionUtils.isEmpty(valMapList)) {
            throw new ErrException(ErrCode.EMPTY_COLLECTION);
        }

        int sqlLength = sqlList.size();
        int valLength = valMapList.size();
        if (sqlLength != valLength) {
            throw new ErrException(ErrCode.INVALID_MATCH);
        }

        return this.baseDAO.executeSQLQuery(sqlList, valMapList);
    }

    /**
     * 执行源生sql语句查询
     *
     * @param operaId        操作员id
     * @param categoryType   类型
     * @param permissionType 权限类型
     * @param resCode        资源的唯一代码
     * @param sqlList        源生语句
     * @param valMapList     参数，名值对应
     * @return 返回对象列表
     * @throws ErrException
     */
    public List<List<Object>> executeSQLQuery(String operaId, int categoryType, int permissionType, long resCode, List<String> sqlList, List<Map<String, Object>> valMapList) throws ErrException {

        this.hasPermission(operaId, categoryType, permissionType, resCode);

        return this.executeSQLQuery(sqlList, valMapList);
    }

    /**
     * 执行源生sql语句,主要用于更新或者新增
     *
     * @param sql
     * @return 已执行的数量
     * @throws ErrException
     */
    public int executeSQL(String sql) throws ErrException {

        if (StringUtils.isBlank(sql)) {
            throw new ErrException(ErrCode.BLANK_STR);
        }
        return this.baseDAO.executeSQL(sql);
    }

    /**
     * 执行源生sql语句,主要用于更新或者新增
     *
     * @param operaId        操作员id
     * @param categoryType   类型
     * @param permissionType 权限类型
     * @param resCode        资源的唯一代码
     * @param sql            源生语句
     * @return 已执行条数
     * @throws ErrException
     */
    public int executeSQL(String operaId, int categoryType, int permissionType, long resCode, String sql) throws ErrException {
        this.hasPermission(operaId, categoryType, permissionType, resCode);

        return this.executeSQL(sql);

    }

    /**
     * 执行源生sql语句,主要用于更新或者新增
     *
     * @param sqlList
     * @return 已执行的数量
     * @throws ErrException
     */
    public int executeSQL(List<String> sqlList) throws ErrException {
        if (CollectionUtils.isEmpty(sqlList)) {
            throw new ErrException(ErrCode.EMPTY_COLLECTION);
        }

        return this.baseDAO.executeSQL(sqlList);
    }

    /**
     * 执行源生sql语句,主要用于更新或者新增
     *
     * @param operaId        操作员id
     * @param categoryType   类型
     * @param permissionType 权限类型
     * @param resCode        资源的唯一代码
     * @param sqlList        源生语句
     * @return 已执行条数
     * @throws ErrException
     */
    public int executeSQL(String operaId, int categoryType, int permissionType, long resCode, List<String> sqlList) throws ErrException {

        this.hasPermission(operaId, categoryType, permissionType, resCode);

        return this.executeSQL(sqlList);
    }

    /**
     * 执行sql语句,主要用于更新或者新增
     *
     * @param sql
     * @param valMap
     * @return
     * @throws ErrException
     */
    public int executeSQL(String sql, Map<String, Object> valMap) throws ErrException {
        if (StringUtils.isBlank(sql)) {
            throw new ErrException(ErrCode.BLANK_STR);
        }

        if (CollectionUtils.isEmpty(valMap)) {
            throw new ErrException(ErrCode.EMPTY_COLLECTION);
        }

        return this.baseDAO.executeSQL(sql, valMap);

    }

    /**
     * 执行sql语句,主要用于更新或者新增
     *
     * @param operaId        操作员id
     * @param categoryType   类型
     * @param permissionType 权限类型
     * @param resCode        资源的唯一代码
     * @param sql
     * @return 已执行条数
     * @throws ErrException
     */
    public int executeSQL(String operaId, int categoryType, int permissionType, long resCode, String sql, Map<String, Object> valMap) throws ErrException {

        this.hasPermission(operaId, categoryType, permissionType, resCode);

        return this.executeSQL(sql, valMap);

    }

    /**
     * 执行源生sql语句,主要用于更新或者新增
     *
     * @param sqlList
     * @param valMapList 参数，这里sqls与valMapList一一对应
     * @return
     * @throws ErrException
     */
    public int executeSQL(List<String> sqlList, List<Map<String, Object>> valMapList) throws ErrException {
        if (CollectionUtils.isEmpty(sqlList)) {
            throw new ErrException(ErrCode.EMPTY_COLLECTION);
        }

        if (CollectionUtils.isEmpty(valMapList)) {
            throw new ErrException(ErrCode.EMPTY_COLLECTION);
        }

        int sqlLength = sqlList.size();
        int valLength = valMapList.size();
        if (sqlLength != valLength) {
            throw new ErrException(ErrCode.INVALID_MATCH);
        }

        return this.baseDAO.executeSQL(sqlList, valMapList);

    }

    /**
     * 执行hql语句,包含jpql语句
     *
     * @param operaId        操作员id
     * @param categoryType   类型
     * @param permissionType 权限类型
     * @param resCode        资源的唯一代码
     * @param sqlList        sql语句
     * @param valMapList     参数列表，这里sqls与valMapList顺序一一对应
     * @return 已执行条数
     * @throws ErrException
     */
    public int executeSQL(String operaId, int categoryType, int permissionType, long resCode, List<String> sqlList, List<Map<String, Object>> valMapList) throws ErrException {

        this.hasPermission(operaId, categoryType, permissionType, resCode);

        return this.executeSQL(sqlList, valMapList);

    }


    /**
     * 执行hql语句,包含jpql语句
     *
     * @param hql
     * @return 已执行条数
     * @throws ErrException
     */
    public int executeHQL(String hql) throws ErrException {
        if (StringUtils.isBlank(hql)) {
            throw new ErrException(ErrCode.BLANK_STR);
        }
        return this.baseDAO.executeHQL(hql);
    }

    /**
     * 执行hql语句,包含jpql语句
     *
     * @param operaId        操作员id
     * @param categoryType   类型
     * @param permissionType 权限类型
     * @param resCode        资源的唯一代码
     * @param hql            hql,jpql语句
     * @return 已执行条数
     * @throws ErrException
     */
    public int executeHQL(String operaId, int categoryType, int permissionType, long resCode, String hql) throws ErrException {
        this.hasPermission(operaId, categoryType, permissionType, resCode);

        return this.executeHQL(hql);
    }

    /**
     * 执行hql语句,包含jpql语句
     *
     * @param hqls
     * @return 已执行条数
     * @throws ErrException
     */
    public int executeHQL(List<String> hqls) throws ErrException {
        if (CollectionUtils.isEmpty(hqls)) {
            throw new ErrException(ErrCode.EMPTY_COLLECTION);
        }
        return this.baseDAO.executeHQL(hqls);
    }

    /**
     * 执行hql语句,包含jpql语句
     *
     * @param operaId        操作员id
     * @param categoryType   类型
     * @param permissionType 权限类型
     * @param resCode        资源的唯一代码
     * @param hqlList        hql,jpql语句
     * @return 已执行条数
     * @throws ErrException
     */
    public int executeHQL(String operaId, int categoryType, int permissionType, long resCode, List<String> hqlList) throws ErrException {

        this.hasPermission(operaId, categoryType, permissionType, resCode);

        return this.executeHQL(hqlList);
    }

    /**
     * 执行hql语句,包含jpql语句
     *
     * @param hql
     * @param valMap
     * @return
     * @throws ErrException
     */
    public int executeHQL(String hql, Map<String, Object> valMap) throws ErrException {
        if (StringUtils.isBlank(hql)) {
            throw new ErrException(ErrCode.BLANK_STR);
        }

        if (CollectionUtils.isEmpty(valMap)) {
            throw new ErrException(ErrCode.EMPTY_COLLECTION);
        }

        return this.baseDAO.executeHQL(hql, valMap);

    }

    /**
     * 执行hql语句,包含jpql语句
     *
     * @param operaId        操作员id
     * @param categoryType   类型
     * @param permissionType 权限类型
     * @param resCode        资源的唯一代码
     * @param hql            hql,jpql语句
     * @param valMap         参数，名值对应
     * @return 已执行条数
     * @throws ErrException
     */
    public int executeHQL(String operaId, int categoryType, int permissionType, long resCode, String hql, Map<String, Object> valMap) throws ErrException {
        this.hasPermission(operaId, categoryType, permissionType, resCode);

        return this.executeHQL(hql, valMap);

    }

    /**
     * 执行hql语句,包含jpql语句
     *
     * @param hqlList
     * @param valMapList
     * @return
     * @throws ErrException
     */
    public int executeHQL(List<String> hqlList, List<Map<String, Object>> valMapList) throws ErrException {
        if (CollectionUtils.isEmpty(hqlList)) {
            throw new ErrException(ErrCode.EMPTY_COLLECTION);
        }

        if (CollectionUtils.isEmpty(valMapList)) {
            throw new ErrException(ErrCode.EMPTY_COLLECTION);
        }

        int hqlLength = hqlList.size();
        int valLength = valMapList.size();
        if (hqlLength != valLength) {
            throw new ErrException(ErrCode.INVALID_MATCH);
        }

        return this.baseDAO.executeHQL(hqlList, valMapList);
    }


    /**
     * 执行hql语句,包含jpql语句
     *
     * @param operaId        操作员id
     * @param categoryType   类型
     * @param permissionType 权限类型
     * @param resCode        资源的唯一代码
     * @param hqlList        hql,jpql语句
     * @param valMapList     参数列表，这里sqls与valMapList顺序一一对应
     * @return 已执行条数
     * @throws ErrException
     */
    public int executeHQL(String operaId, int categoryType, int permissionType, long resCode, List<String> hqlList, List<Map<String, Object>> valMapList) throws ErrException {
        this.hasPermission(operaId, categoryType, permissionType, resCode);

        return this.executeHQL(hqlList, valMapList);
    }

    /**
     * 创建实体
     *
     * @param entity
     * @throws ErrException
     */
    public void create(BaseEntity entity) throws ErrException {
        if (null == entity) {
            throw new ErrException(ErrCode.NULL_OBJ);
        }

        if (!StringUtils.isBlank(entity.getId())) {
            throw new ErrException(ErrCode.EXIST_ID);
        }

        try {
            this.baseDAO.save(entity);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new ErrException(ErrCode.CREATE_FAILED);
        }

    }

    /**
     * 创建对象
     *
     * @param operaId        管理员id
     * @param categoryType   权限对应的领域
     * @param permissionType 权限类型
     * @param resCode        资源代码
     * @param entity         待保存的对象
     * @throws ErrException
     */
    public void create(String operaId, int categoryType, int permissionType, long resCode, BaseEntity entity) throws ErrException {

        this.hasPermission(operaId, categoryType, permissionType, resCode);

        try {
            this.create(entity);

        } catch (Exception ex) {
            ex.printStackTrace();
            throw new ErrException(ErrCode.CREATE_FAILED);
        }
    }

    /**
     * 删除实体
     *
     * @param c
     * @param id
     * @throws ErrException
     */
    public void del(Class c, String id) throws ErrException {

        if (null == c) {
            throw new ErrException(ErrCode.NULL_OBJ);
        }

        if (StringUtils.isBlank(id)) {
            throw new ErrException(ErrCode.BLANK_ID);
        }

        if (!this.isExsit(c, id)) {
            throw new ErrException(ErrCode.NO_ENTITY);
        }

        try {
            this.baseDAO.delOfLogicalById(c, id);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new ErrException(ErrCode.DEL_FAILED);
        }

    }

    /**
     * 删除对象
     *
     * @param operaId        管理员id
     * @param categoryType   权限对应的领域
     * @param permissionType 权限类型
     * @param resCode        资源代码
     * @param c
     * @param id
     * @throws ErrException
     */
    public void del(String operaId, int categoryType, int permissionType, long resCode, Class c, String id) throws ErrException {

        this.hasPermission(operaId, categoryType, permissionType, resCode);

        try {

            this.del(c, id);

        } catch (Exception ex) {
            ex.printStackTrace();
            throw new ErrException(ErrCode.DEL_FAILED);
        }
    }

    /**
     * 更新对象
     *
     * @param entity
     * @throws ErrException
     */
    public void update(BaseEntity entity) throws ErrException {
        if (null == entity) {
            throw new ErrException(ErrCode.NULL_OBJ);
        }

        if (StringUtils.isBlank(entity.getId())) {
            throw new ErrException(ErrCode.BLANK_ID);
        }

        if (!this.isExsit(entity.getClass(), entity.getId())) {
            throw new ErrException(ErrCode.NO_ENTITY);
        }

        try {
            this.baseDAO.update(entity);
        } catch (Exception ex) {
            throw new ErrException(ErrCode.UPDATE_FAILED);
        }
    }

    /**
     * 更新对象
     *
     * @param operaId        管理员id
     * @param categoryType   权限对应的领域
     * @param permissionType 权限类型
     * @param resCode        资源代码
     * @param entity
     * @throws ErrException
     */
    public void update(String operaId, int categoryType, int permissionType, long resCode, BaseEntity entity) throws ErrException {
        this.hasPermission(operaId, categoryType, permissionType, resCode);

        try {
            this.update(entity);
        } catch (Exception ex) {
            throw new ErrException(ErrCode.UPDATE_FAILED);
        }

    }

    /**
     * 查询对象
     *
     * @param c
     * @param id
     * @return
     * @throws ErrException
     */
    public BaseEntity find(Class c, String id) throws ErrException {
        if (null == c) {
            throw new ErrException(ErrCode.NULL_OBJ);
        }

        if (StringUtils.isBlank(id)) {
            throw new ErrException(ErrCode.BLANK_ID);
        }

        try {
            return this.baseDAO.findUnique(c, id);
        } catch (Exception ex) {
            throw new ErrException(ErrCode.NO_ENTITY);
        }
    }

    /**
     * 查询对象
     *
     * @param operaId        管理员id
     * @param categoryType   权限对应的领域
     * @param permissionType 权限类型
     * @param resCode        资源代码
     * @param c
     * @param id
     * @return
     * @throws ErrException
     */
    public BaseEntity find(String operaId, int categoryType, int permissionType, long resCode, Class c, String id) throws ErrException {

        this.hasPermission(operaId, categoryType, permissionType, resCode);

        try {
            return this.find(c, id);
        } catch (Exception ex) {
            throw new ErrException(ErrCode.NO_ENTITY);
        }
    }

    /**
     * 从数据库中重新获取对象
     *
     * @param entity
     */
    public void refresh(BaseEntity entity) throws ErrException {
        if (null == entity) {
            throw new ErrException(ErrCode.NULL_OBJ);
        }

        if (StringUtils.isBlank(entity.getId())) {
            throw new ErrException(ErrCode.BLANK_ID);
        }

        if (!this.isExsit(entity.getClass(), entity.getId())) {
            throw new ErrException(ErrCode.NO_ENTITY);
        }
        this.baseDAO.refresh(entity);
    }

    /**
     * 从数据库中重新获取对象
     *
     * @param operaId        管理员id
     * @param categoryType   权限对应的领域
     * @param permissionType 权限类型
     * @param resCode        资源代码
     * @param entity
     * @throws ErrException
     */
    public void refresh(String operaId, int categoryType, int permissionType, long resCode, BaseEntity entity) throws ErrException {
        this.hasPermission(operaId, categoryType, permissionType, resCode);

        try {
            this.refresh(entity);
        } catch (Exception ex) {
            throw new ErrException(ErrCode.NO_ENTITY);
        }
    }

    /**
     * 查询对象列表
     *
     * @param queryHql
     * @return
     * @throws ErrException
     */
    public List<Map<String, Object>> queryList(String queryHql) throws ErrException {
        if (StringUtils.isBlank(queryHql)) {
            throw new ErrException(ErrCode.BLANK_STR);
        }
        try {
            return this.baseDAO.find(queryHql);
        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    /**
     * 查询对象列表
     *
     * @param operaId        管理员id
     * @param categoryType   权限对应的领域
     * @param permissionType 权限类型
     * @param resCode        资源代码
     * @param queryHql
     * @return
     * @throws ErrException
     */
    public List<Map<String, Object>> queryList(String operaId, int categoryType, int permissionType, long resCode, String queryHql) throws ErrException {

        this.hasPermission(operaId, categoryType, permissionType, resCode);

        try {
            return this.queryList(queryHql);
        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    /**
     * 查询对象列表，设定从第几条记录开始查询
     *
     * @param queryHql
     * @param firstIndex 查询的起始位置
     * @return
     * @throws ErrException
     */
    public List<Map<String, Object>> queryList(String queryHql, int firstIndex) throws ErrException {

        if (StringUtils.isBlank(queryHql)) {
            throw new ErrException(ErrCode.BLANK_STR);
        }
        try {
            return this.baseDAO.find(queryHql, firstIndex);
        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }

    }

    /**
     * 查询对象列表，设定从第几条记录开始查询
     *
     * @param operaId        管理员id
     * @param categoryType   权限对应的领域
     * @param permissionType 权限类型
     * @param resCode        资源代码
     * @param queryHql
     * @param firstIndex     查询的起始位置
     * @return
     * @throws ErrException
     */
    public List<Map<String, Object>> queryList(String operaId, int categoryType, int permissionType, long resCode, String queryHql, int firstIndex) throws ErrException {

        this.hasPermission(operaId, categoryType, permissionType, resCode);

        try {
            return this.queryList(queryHql, firstIndex);
        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    /**
     * 查询对象列表
     *
     * @param queryHql
     * @param firstIndex 查询的起始位置
     * @param size       需要查询的数量
     * @return
     * @throws ErrException
     */
    public List<Map<String, Object>> queryList(String queryHql, int firstIndex, int size) throws ErrException {

        if (StringUtils.isBlank(queryHql)) {
            throw new ErrException(ErrCode.BLANK_STR);
        }

        if (ConfigUtil.getInt(ConfigUtil.WRONG_NUM) == size) {
            throw new ErrException(ErrCode.INVALID_NUM);
        }

        try {
            return this.baseDAO.find(queryHql, firstIndex, size);
        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }

    }

    /**
     * 查询对象列表，设定从第几条记录开始查询
     *
     * @param operaId        管理员id
     * @param categoryType   权限对应的领域
     * @param permissionType 权限类型
     * @param resCode        资源代码
     * @param queryHql
     * @param firstIndex     查询的起始位置
     * @param size           需要查询的数量
     * @return
     * @throws ErrException
     */
    public List<Map<String, Object>> queryList(String operaId, int categoryType, int permissionType, long resCode, String queryHql, int firstIndex, int size) throws ErrException {

        this.hasPermission(operaId, categoryType, permissionType, resCode);

        try {
            return this.queryList(queryHql, firstIndex, size);
        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    /**
     * 判断某条实体是否被删除，这里只查询逻辑删除
     *
     * @param c
     * @param id
     * @return
     * @throws ErrException
     */
    public boolean isDeleted(Class c, String id) throws ErrException {
        if (StringUtils.isBlank(id)) {
            throw new ErrException(ErrCode.BLANK_ID);
        }

        BaseEntity entity = baseDAO.findUnique(c, id);

        if (null == entity) {
            throw new ErrException(ErrCode.NO_ENTITY);
        }

        return entity.isDeleted();
    }

    /**
     * 是否存在某条实体，不能查询已被逻辑删除的实体
     *
     * @param c
     * @param id
     * @return
     * @throws ErrException
     */
    public boolean isExsit(Class c, String id) throws ErrException {
        if (StringUtils.isBlank(id)) {
            throw new ErrException(ErrCode.BLANK_ID);
        }

        BaseEntity entity = baseDAO.findUnique(c, id);

        if (null == entity) {
            return false;
        } else {
            return true;
        }
    }

    public boolean hasPermission(String operaId, int categoryType, int permissionType, long resCode) throws ErrException{
        try {
            List<Resource> resourceList = this.queryResourceService.findResourcesByUserId(operaId,categoryType,permissionType);

             for(Resource res:resourceList){
                 if(resCode !=ConfigUtil.getLong(ConfigUtil.WRONG_NUM) && res.getResCode() == resCode){
                     return true;
                 }
             }

            return false;

        } catch (ErrException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return false;
    }

}
